<script setup>
import {ElMessage} from "element-plus";
import _ from "lodash";
import {ref} from "vue";
import useWebsiteStore from "@/store/websiteStore";

const websiteStore = useWebsiteStore();

const url = ref("");

const centerDialogVisible = ref(false)
const loading = ref(false)
const onHandlerAdd = async () => {
    if (url.value === "") {
        ElMessage.error('地址不能为空')
        return
    }

    if (_.find(websiteStore.websiteList, {url: url.value})) {
        ElMessage.error('该网址已存在')
        return
    }

    loading.value = true;
    let result = await window.urlApi.sendUrl(url.value);
    console.log(result)
    if (result.status === 0) {
        websiteStore.setWebsiteList(result);
        centerDialogVisible.value = false;
        url.value = "";
    } else {
        ElMessage.error('该网址无法访问，请检查！！！')
    }

    loading.value = false;
}
defineExpose({
    centerDialogVisible,
    url,
})
</script>

<template>
    <el-dialog v-model="centerDialogVisible" title="添加网址" width="30%" center>
        <div>
            <el-input placeholder="请输入地址" clearable :disabled="loading" @keydown.enter="onHandlerAdd()"
                      v-model="url"></el-input>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="centerDialogVisible = false" :disabled="loading">取消</el-button>
                <el-button type="primary" @click="onHandlerAdd()" v-loading="loading">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<style scoped lang="scss">

</style>
